package inconceivable.jiangwen;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateToString {

    /* 转Date */

    /**
     * LocalDateTime转Date
     *
     * @return Date
     */
    public Date localDateTimeToDate() {
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime dateTime = LocalDateTime.now();
        ZonedDateTime zdf = dateTime.atZone(zone);
        return Date.from(zdf.toInstant());
    }

    /**
     * LocalDate转Date
     *
     * @return Date
     */
    public Date LocalDateToDate() {
        ZoneId zone = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.now();
        // 实质上是把LocalDate先转成了LocalDateTime
        ZonedDateTime zdf = localDate.atStartOfDay().atZone(zone);
        return Date.from(zdf.toInstant());
    }

    /**
     * String转Date
     *
     * @return Date
     */
    public Date StringToDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse("2021-01-01 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * long(时间戳)转Date
     *
     * @return Date
     */
    public Date longToDate() {
        long timestamp = System.currentTimeMillis();
        return new Date(timestamp);
    }

    /* 转LocalDateTime */

    /**
     * Date转LocalDateTime
     *
     * @return Date
     */
    public LocalDateTime dateToLocalDateTime() {
        ZoneId zone = ZoneId.systemDefault();
        Date date = new Date();
        return date.toInstant().atZone(zone).toLocalDateTime();
    }

    /**
     * LocalDate转LocalDateTime
     *
     * @return Date
     */
    public LocalDateTime localDateToLocalDateTime() {
        LocalDate localDate = LocalDate.now();
        return localDate.atStartOfDay();
    }

    /**
     * String转LocalDateTime
     *
     * @return Date
     */
    public LocalDateTime stringToLocalDateTime() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse("2021-01-01 00:00:00", df);
    }

    /**
     * long(时间戳)转LocalDateTime
     *
     * @return Date
     */
    public LocalDateTime longToLocalDateTime() {
        ZoneId zone = ZoneId.systemDefault();
        long timestamp = System.currentTimeMillis();
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zone);
    }

    /* 转LocalDate */

    /**
     * Date转LocalDate
     *
     * @return Date
     */
    public LocalDate DateToLocalDate() {
        ZoneId zone = ZoneId.systemDefault();
        Date date = new Date();
        return date.toInstant().atZone(zone).toLocalDate();
    }

    /**
     * LocalDateTime转LocalDate
     *
     * @return Date
     */
    public LocalDate LocalDateTimeToLocalDate() {
        LocalDateTime dateTime = LocalDateTime.now();
        return dateTime.toLocalDate();
    }

    /**
     * String转LocalDate
     *
     * @return Date
     */
    public LocalDate StringToLocalDate() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDate.parse("2021-01-01", df);
    }

    /**
     * long(时间戳)转LocalDate
     *
     * @return Date
     */
    public LocalDate longToLocalDate() {
        ZoneId zone = ZoneId.systemDefault();
        long timestamp = System.currentTimeMillis();
        return Instant.ofEpochMilli(timestamp).atZone(zone).toLocalDate();
    }

}
